package com.gdth.widget.webview;

import com.gdth.core.widget.BaseActivity;

import android.annotation.TargetApi;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage.QuotaUpdater;
import android.webkit.WebView;

public class BaseWebChromeClient extends WebChromeClient{

	public ValueCallback<Uri> mUploadMessage;
	public ValueCallback<Uri[]> uploadMessage;
	private BaseActivity mActivity;

	public BaseWebChromeClient(BaseActivity mContext){

		this.mActivity = mContext;
	}

	@Override
	public void onReceivedTitle(WebView view, String title) {
		super.onReceivedTitle(view, title);

		if (listener != null) {
			listener.onReceivedTitle(title);
		}
	}

    public void onProgressChanged(WebView view, int progress){
//    	mActivity.setTitle("Loading...");       
//    	mActivity.setProgress(progress * 100);     
//        if(progress == 100)
//        	mActivity.setTitle(R.string.app_name);       
    }

    /**此处覆盖的是javascript中的alert方法。 
     * 当网页需要弹出alert窗口时，会执行onJsAlert中的方法 
     * 网页自身的alert方法不会被调用。 
     */
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//        Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();

//        result.confirm();  
//        return true;  
        return super.onJsAlert(view, url, message, result);
    }

    /**此处覆盖的是javascript中的confirm方法。 
     * 当网页需要弹出confirm窗口时，会执行onJsConfirm中的方法 
     * 网页自身的confirm方法不会被调用。 
     */
    @Override
    public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
        return super.onJsConfirm(view, url, message, result);
    }

    /**此处覆盖的是javascript中的confirm方法。 
     * 当网页需要弹出confirm窗口时，会执行onJsConfirm中的方法 
     * 网页自身的confirm方法不会被调用。 
     */
    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
        return super.onJsPrompt(view, url, message, defaultValue, result);
    }

    /**如果页面被强制关闭,弹窗提示：是否确定离开？ 
     * 点击确定 保存数据离开，点击取消，停留在当前页面 
     */
    @Override
    public boolean onJsBeforeUnload(WebView view, String url,
            String message, JsResult result) {
//    	Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
        result.confirm();
        return true;
    }

    //扩充缓存的容量    
    @TargetApi(Build.VERSION_CODES.ECLAIR)
    @Override
    public void onReachedMaxAppCacheSize(long spaceNeeded,
                long totalUsedQuota, QuotaUpdater quotaUpdater) {
        quotaUpdater.updateQuota(spaceNeeded * 2);
    }

    // For 3.0+ Devices (Start)
    // onActivityResult attached before constructor
    @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
    protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
    {
    	uploadMessage = null;
    	mUploadMessage = uploadMsg;
    	mActivity.showUploadOptions();
    }


    // For Lollipop 5.0+ Devices

    @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient fileChooserParams)
    {
        if (uploadMessage != null) {
            uploadMessage.onReceiveValue(null);
            uploadMessage = null;
        }
        mUploadMessage = null;
        uploadMessage = filePathCallback;
        mActivity.showUploadOptions();
        return true;
    }

    //For Android 4.1 only
    @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
    protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
    {
    	openFileChooser(uploadMsg, acceptType);
    }

    @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
    protected void openFileChooser(ValueCallback<Uri> uploadMsg)
    {
    	openFileChooser(uploadMsg,"");
    }

    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog,
            boolean isUserGesture, Message resultMsg) {
    	WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
		transport.setWebView(view);    //此webview可以是一般新创建的
		resultMsg.sendToTarget();

		return false;
    }

    private WebChromeClientListener listener;

    public void setWebChromeClientListener(WebChromeClientListener listener) {
    	this.listener = listener;
    }

    public interface WebChromeClientListener{
    	void onReceivedTitle(String title);
    }
}
